package ru.compscicenter.uno.wizard;

import ru.compscicenter.uno.GameSettings;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.io.IOException;

/**
 * User: SLeeP
 * Date: 05.03.12
 * Time: 0:31
 */
public class Panel2ClientDescriptor extends WizardPanelDescriptor implements DocumentListener {
    public static final String IDENTIFIER = "SERVER_IP_CHOOSE";
    private String next;
    private Panel2Client panel2Client;
    private GameSettings gameSettings;

    public Panel2ClientDescriptor(GameSettings gameSettings) {
        this.gameSettings = gameSettings;
        try {
            panel2Client = new Panel2Client(gameSettings);
            panel2Client.addIpChangeListener(this);
        } catch (IOException e) {
            e.printStackTrace();
        }
        setPanelDescriptorIdentifier(IDENTIFIER);
        setPanelComponent(panel2Client);
    }

    public Object getNextPanelDescriptor() {
        checkIP();
        return Panel3ClientRulesDescriptor.IDENTIFIER;

    }

    public Object getBackPanelDescriptor() {
        return Panel1Descriptor.IDENTIFIER;
    }

    private void checkIP() {
        gameSettings.address = panel2Client.getAddress();
        if (panel2Client.checkIP(gameSettings.address)) {
            Panel4ClientWait.myIPtext.setText(gameSettings.address);
            getWizard().setNextFinishButtonEnabled(true);
        } else {
            getWizard().setNextFinishButtonEnabled(false);
        }
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        checkIP();
        panel2Client.setListNoChosen();
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        checkIP();
        panel2Client.setListNoChosen();
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        checkIP();
        panel2Client.setListNoChosen();
    }

    public void updateAvailableServers() {
        panel2Client.updateAvailableServers();
    }
}
